﻿using ChestPainCenterSystem.Areas.ChestPainCenter.Models;
using ChestPainCenterSystem.Bll;
using ChestPainCenterSystem.Core;
using ChestPainCenterSystem.ThreeCenterManage;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace ChestPainCenterSystem.Code.Bll
{
    /// <summary>
    /// 诊疗路径相关
    /// </summary>
    public class PathwayManager : BaseManager
    {
        /// <summary>
        /// 获取诊疗路径列表
        /// </summary>
        /// <param name="pathName"></param>
        /// <returns></returns>
        public static OperateResult GetDiagnosticPath(int pageIndex,int pageSize, string pathName)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                ThreeCenterManageSoapClient service = new ThreeCenterManageSoapClient();
                ResultHelperOfString result = service.GetDiagnosticPath(pathName,pageIndex,pageSize);
                if (result.S)
                {
                    resultInfo.Result = 1;
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 获取诊疗路径名称列表
        /// ID Name 
        /// </summary>
        /// <returns></returns>
        public static OperateResult GetPathNameList()
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                ThreeCenterManageSoapClient service = new ThreeCenterManageSoapClient();
                ResultHelperOfString result = service.GetPathNameList();
                if (result.S)
                {
                    resultInfo.Result = 1;
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }            
            return resultInfo;
        }

        /// <summary>
        /// 新增或编辑诊疗路径 依据ID
        /// </summary>
        /// <param name="pathJson"></param>
        /// <returns></returns>
        public static OperateResult SavePathName(string pathJson) 
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                ThreeCenterManageSoapClient service = new ThreeCenterManageSoapClient();               
                ResultHelperOfBoolean result = service.SetDiagnosticPath(pathJson);
                if (result.S)
                {
                    resultInfo.Result = 1;
                    resultInfo.Body = result.M;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }

            return resultInfo;
        }

        /// <summary>
        /// 返回诊疗路径对象
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static OperateResult GetDiagnosticPathById(int Id)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                ThreeCenterManageSoapClient service = new ThreeCenterManageSoapClient();
                ResultHelperOfString result = service.GetDiagnosticPathById(Id);
                if (result.S)
                {
                    resultInfo.Result = 1;
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }

            return resultInfo;
        }

        /// <summary>
        /// 根据诊疗路径ID获取模板或内容
        /// </summary>
        /// <param name="pathId"></param>
        /// <param name="patientId"></param>
        /// <returns></returns>
        public static OperateResult GetDiagnosticPathTemp(int pathId, string patientId) 
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                ThreeCenterManageSoapClient service = new ThreeCenterManageSoapClient();
                ResultHelperOfString result = service.GetDiagnosticPathTemp(pathId, patientId);
                if (result.S)
                {
                    resultInfo.Result = 1;
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 保存诊疗路径模板
        /// </summary>
        /// <param name="contentJson"></param>
        /// <returns></returns>
        public static OperateResult SetDiagnosticPathTemp(string contentJson) 
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                ThreeCenterManageSoapClient service = new ThreeCenterManageSoapClient();
                ResultHelperOfBoolean result = service.SetDiagnosticPathTemp(contentJson);
                if (result.S)
                {
                    resultInfo.Result = 1;
                    resultInfo.Body = result.M;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 批量删除诊疗路径模板
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static OperateResult DeleteDiagnosticPathTemp (ArrayOfInt idList)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                ThreeCenterManageSoapClient service = new ThreeCenterManageSoapClient();
                ResultHelperOfBoolean result = service.DeleteDiagnosticPathTemp(idList);
                if (result.S)
                {
                    resultInfo.Result = 1;
                    resultInfo.Body = result.M;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }



        /// <summary>
        /// 获取质控列表（所有）
        /// </summary>
        /// <returns></returns>
        public static OperateResult GetQualityPointList(string Id = "")
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                string NameSelect = @"use ThreeCenterManage; 
                            SELECT * FROM [QualityPointSet] where  Status <> 2 ";
                if (!string.IsNullOrEmpty(Id))
                {
                    NameSelect += @" and Id=" + Id;
                }
                NameSelect += @" order by Sort ";
                DataSet NameList = SQLDLL.SqlHelper.ExecuteDataset(NameSelect, System.Data.CommandType.Text);


                if (NameList != null && NameList.Tables != null && NameList.Tables.Count > 0)
                {
                    DataTable dt = NameList.Tables[0];
                    resultInfo.Body = dt;
                    resultInfo.Result = 1;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = "查询失败";
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 获取子集字段列表
        /// </summary>
        /// <returns></returns>
        public static OperateResult GetTableColumnList()
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                string NameSelect = @"use ThreeCenterManage; 
                            SELECT * FROM [TableColumn] where  Status <> 2 ";
                DataSet NameList = SQLDLL.SqlHelper.ExecuteDataset(NameSelect, System.Data.CommandType.Text);


                if (NameList != null && NameList.Tables != null && NameList.Tables.Count > 0)
                {
                    DataTable dt = NameList.Tables[0];
                    resultInfo.Body = dt;
                    resultInfo.Result = 1;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = "查询失败";
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 保存质控项
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="QualityPointTitle"></param>
        /// <param name="ComparePointId"></param>
        /// <param name="OverTime"></param>
        /// <param name="Sort"></param>
        /// <param name="Status"></param>
        /// <param name="SetUserId"></param>
        /// <returns></returns>
        public static OperateResult SaveQualityPoint(QualityPoint qp)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                string NameSelect = string.Empty;
                if (qp.Id != "0" && !string.IsNullOrEmpty(qp.Id))
                {
                    NameSelect = @"use ThreeCenterManage; 
                            update [QualityPointSet] 
                                set  QualityPointTitle='" + qp.QualityPointTitle + @"',
                                QualityPointName='" + qp.QualityPointName + @"',
                                QualityPointTableName='" + qp.QualityPointTableName + @"',
                                ComparePointTitle='" + qp.ComparePointTitle + @"',
                                ComparePointName='" + qp.ComparePointName + @"',
                                ComparePointTableName='" + qp.ComparePointTableName + @"',
                                OverTime=" + qp.OverTime + @",
                                Sort=" + qp.Sort + @",
                                Status=" + qp.Status + @",
                                Title='" + qp.Title + @"',
                                QualityPointTCId=" + qp.QualityPointTCId + @",
                                ComparePointTCId=" + qp.ComparePointTCId + @"
                            where Id=" + qp.Id;
                }
                else
                {
                    NameSelect = @"use ThreeCenterManage; 
                        INSERT INTO dbo.QualityPointSet
                        (
                            QualityPointTitle,
                            QualityPointName,
                            QualityPointTableName,
                            ComparePointTitle,
                            ComparePointName,
                            ComparePointTableName,
                            OverTime,
                            Sort,
                            Status,
                            Title,
                            QualityPointTCId,
                            ComparePointTCId
                        )
                        VALUES
                        (   N'" + qp.QualityPointTitle + @"', -- QualityPointTitle - nvarchar(100)
                            N'" + qp.QualityPointName + @"', -- QualityPointName - nvarchar(100)
                            N'" + qp.QualityPointTableName + @"', -- QualityPointTableName - nvarchar(50)
                            N'" + qp.ComparePointTitle + @"', -- ComparePointTitle - nvarchar(100)
                            N'" + qp.ComparePointName + @"', -- ComparePointName - nvarchar(100)
                            N'" + qp.ComparePointTableName + @"', -- ComparePointTableName - nvarchar(50)
                            " + qp.OverTime + @",   -- OverTime - int
                            " + qp.Sort + @",   -- Sort - int
                            " + qp.Status + @",   -- Status - int
                            N'" + qp.Title + @"', -- Title - nvarchar(50)
                            " + qp.QualityPointTCId + @",   -- QualityPointTCId - int
                            " + qp.ComparePointTCId + @"    -- ComparePointTCId - int
                            )";
                }
                int rst = SQLDLL.SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text ,NameSelect );
                if (rst > 0)
                {
                    resultInfo.Result = 1;
                    resultInfo.Body = "保存成功";
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = "保存失败";
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }


        /// <summary>
        /// 质控项状态变化
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public static OperateResult ChangeStatus(string Id, string Status)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                string NameSelect = @"use ThreeCenterManage; 
                            update [QualityPointSet] set Status =  " + Status + " where Id=" + Id;
                int rst = SQLDLL.SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, NameSelect);


                if (rst > 0)
                {
                    resultInfo.Result = 1;
                    resultInfo.Body = "保存成功";
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = "保存失败";
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }


        /// <summary>
        /// 获取已开启的质控项列表
        /// </summary>
        /// <param name="PatientID"></param>
        /// <returns></returns>
        public static OperateResult GetOpenQuanlityPointList(string PatientID)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                ThreeCenterManageSoapClient service = new ThreeCenterManageSoapClient();
                ResultHelperOfDataCompleteTimeLine result = service.GetTimeline(PatientID);
                if (result.S)
                {
                    resultInfo.Result = 1;
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }
    }
}